package gameobjects
{
	import assets.Globals;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import flashx.textLayout.conversion.FormatDescriptor;
	
	import logic.Money;
	import logic.Score;

	public class GameObject extends Sprite
	{
		protected var maxHP:Number = 100;
		protected var hp:Number = 100;
		protected var speed:Number = 1;
		protected var graphic:Sprite = new Sprite();
		
		protected var value:Number = 1;
		
		public function GameObject(hp:Number)
		{
			this.hp = this.maxHP = hp;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		protected function init(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function destroy():void
		{
			if (this.parent != Globals.game)
			{
				// nepremostiv BAG
				x = 0;
				y = -100;
				alpha = 0;
			}
			else Globals.game.removeChild(this);
		}
		
		/**
		 * Collision shape sprite;
		 */
		public const cs:Sprite = new Sprite();
		
		/**
		 * Smanjuje HP objektu za prosledjenu vrednodst i
		 * unistava ga ukoliko je HP dosao do nule.
		 * 
		 * Vraca true, ako je objekat unisten.
		 */
		public function decreaseHealth(by:Number):Boolean
		{
			var dead:Boolean = false;
			hp -= by;
			if (hp <= 0)
			{
				dead = true;
				if (this is Enemy) {
					Money.incMoney(value);
					Score.incEnemiesiKilled();
				}
				if (this is Tower) {
					Money.decMoney(value);
					Score.incTowersDestroyed();
				}
				destroy();
			}
			return dead;
		}
		
		protected var fieldX:int, fieldY:int;
		
		public function setFieldX(x:int):void
		{
			fieldX = x;
		}
		public function setFieldY(y:int):void
		{
			fieldY = y;
		}
		
		public function getHealth():Number 
		{
			return hp;
		}
		
		public function getMaxHealth():Number
		{
			return maxHP;
		}
		
		public function getValue():Number
		{
			return value;
		}
	}
}